<?php

namespace Biz\service\Album;

use Biz\model\Album\AlbumListModel;
use Doraemon\cache\AlbumCounter;
use Doraemon\service\es\EsMainContent;
use Doraemon\service\User;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log;

class SyncEsMainContentService
{

    const QUALITY_PENDING = 0;
    const QUALITY_FEATURED = 1;
    const QUALITY_HIGH = 2;
    const QUALITY_ORDINARY = 3;
    const QUALITY_LOW = 4;

    // 这里做了转化 和note看齐 为了统一quality
    const QUALITY_DESC = [
        AlbumListModel::QUALITY_EXCELLENT => self::QUALITY_FEATURED,  //精选,
        AlbumListModel::QUALITY_GOOD => self::QUALITY_HIGH,  //优质,
        AlbumListModel::QUALITY_COMMON => self::QUALITY_ORDINARY,  //普通,
        AlbumListModel::QUALITY_BAD => self::QUALITY_LOW,  //差,
    ];

    /**
     * @param $id
     * @param $albumInfo
     * @return bool
     */
    public static function syncAlbumToEsMainContent($id, $albumInfo): bool
    {
        if ($id && $albumInfo) {
            $uid = IdParse::getUid($id);
            $type = IdParse::getType($id);
            $counter = AlbumCounter::getOne($id);
            $user_type = User::getUser($uid)['type'];
            $quality = 0;
            if ($albumInfo['quality'] > 0) {
                $quality = self::QUALITY_DESC[$albumInfo['quality']];
            }
            // 这里由于看别人的时候 会过滤show_status=5以及11的  所以 定时发布的以及仅自己可见的show_status=5
            $show_status = 1;
            if (in_array($albumInfo['status'], [AlbumListModel::STATUS_DRAFT, AlbumListModel::STATUS_USER_PRIVATE])) {
                $show_status = 5;
            }
            $inputData = [
                'uid' => (int)$uid,
                'type' => (int)$type,
                'user_type' => (int)$user_type,
                'title' => $albumInfo['title'],
                'content' => $albumInfo['desc'],
                'admin_score' => $albumInfo['score'],
                'addtime' => $albumInfo['publish_time'],
                'status' => $albumInfo['status'],
                'like' => $counter['like'],
                'share' => $counter['share'],
                'favorite' => $counter['favorite'],
                'comment' => $counter['comment'],
                'read' => $counter['read'],
                'interact_num' => $counter['like'] + $counter['favorite'] + $counter['comment'] + $counter['share'],
                'quality' => $quality,
                'show_status' => $show_status,
            ];
            $rs = EsMainContent::post($id, $inputData);
            Log::debug("\Biz\service\Album\SyncEsMainContentService::syncAlbumToEsMainContent",
                ['id' => $id, 'input' => $inputData, 'rs' => $rs]);

            return $rs;
        } else {
            Log::debug("syncAlbumToEsMainContent 参数为空",
                ['id' => '', 'input' => [], 'rs' => false]);

            return false;
        }
    }

}